package com.sita.bluetactivity.MineFragment;

import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sita.bluetactivity.Base.BaseApplication;
import com.sita.bluetactivity.Base.BaseValue;
import com.sita.bluetactivity.Base.SpUtils;
import com.sita.bluetactivity.MyClass.BindVehicle;
import com.sita.bluetactivity.MyClass.DaoSingleTon;
import com.sita.bluetactivity.NewBlueTooth.BindBackBean;
import com.sita.bluetactivity.NewBlueTooth.BindVehicleMsg;
import com.sita.bluetactivity.R;
import com.sita.bluetactivity.http.RestClient;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Jason on 2016/10/10.
 */

public class BindVehicleAdapter extends RecyclerView.Adapter<BindVehicleAdapter.MyViewHolder> {
    private Context context;
    private List<BindListBackBean.DataBean> data;
    private ItemClickListener listener;


    public BindVehicleAdapter(Context context,ItemClickListener itemClickListener) {
        this.context = context;
        data = new ArrayList<>();
        listener = itemClickListener;

    }
    public void getData(List<BindListBackBean.DataBean> backBean){
        data =backBean ;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_bindvehicle,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        for (int i=0;i<data.size();i++){

            if (data.get(position).getController_id().equals(SpUtils.getString("nowVehicle",null,BaseApplication.getContext()))){

                holder.Vehicle_state.setText("当前车辆");
                holder.Vehicle_state.setTextColor(BaseApplication.getContext().getResources().getColor(R.color.main_bg));
            }

            else {

                holder.Vehicle_state.setText("切换");
                holder. Vehicle_state.setTextColor(BaseApplication.getContext().getResources().getColor(R.color.main_tab_tx));
            }
        }


         holder.position = position;
         holder.Vehicle_sn_num.setText(data.get(position).getController_id());
    }

    @Override
    public int getItemCount() {
        if (data!=null) {
            return data.size();
        }
        else {
            return 0;
        }
    }


    class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView Vehicle_sn_num,Vehicle_unbind,Vehicle_state;
        private int position;
        private RelativeLayout item_layout;
        public MyViewHolder(View itemView) {
            super(itemView);
            Vehicle_sn_num = (TextView) itemView.findViewById(R.id.vehicle_sn_number);
            Vehicle_unbind = (TextView) itemView.findViewById(R.id.unbind_vehicle);
            item_layout = (RelativeLayout) itemView.findViewById(R.id.item_layout);
            item_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.create();
                    builder.setMessage("确认切换?");
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            builder.create().dismiss();
                        }
                    });
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            builder.create().dismiss();

                            Vehicle_state.setText("当前车辆");
                            Vehicle_state.setTextColor(BaseApplication.getContext().getResources().getColor(R.color.main_bg));

                            BaseValue.Index = position;
                            notifyDataSetChanged();

                            //将车辆ID存储起来为了在行程使用
                            SpUtils.putString("nowVehicle",data.get(position).getController_id(),BaseApplication.getContext());
                            //   Log.e("sssxxxxxx",SpUtils.getString("nowVehicle",null,BaseApplication.getContext()));
                            SpUtils.putString("pass",data.get(position).getController_pass(), BaseApplication.getContext());
                            //存储波导ID
                            SpUtils.putString("BoDaoID",data.get(position).getBodaoid(),BaseApplication.getContext());
                            Log.e("sssssssssssssss",data.get(position).getBodaoid());
                        }
                    });
                    builder.show();


                }
            });
            Vehicle_unbind.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(position);
                    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setCancelable(true);

                    builder.setMessage("确认解绑车辆？");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            UnBindVehicleRequest request = new UnBindVehicleRequest();
                            request.controller_id = data.get(position).getController_id();
                            request.user_id = SpUtils.getString("AccountId",null,BaseApplication.getContext());
                            RestClient.getRestService().UnBindVehicle(request, new Callback<UnBindBackBean>() {
                                @Override
                                public void success(UnBindBackBean unBindBackBean, Response response) {
                                    data.remove(position);
//删除解绑车辆
                                    DaoSingleTon.getInstance().getBindVehicleDao().deleteByKey(Long.valueOf(position+1));

                                    notifyDataSetChanged();
                                    builder.create().dismiss();
                                }

                                @Override
                                public void failure(RetrofitError error) {
                                    Log.e("sssffdsf",error.toString());
                                    builder.create().dismiss();
                                }
                            });


                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                       builder.create().dismiss();
                        }
                    });
                    builder.show();
                        }
                    });


            Vehicle_state = (TextView) itemView.findViewById(R.id.vehicl_state);
        }
    }

}
